/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.help.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.help.model.HelpFolder;
import org.crystalrange.help.service.HelpFolderService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class HelpFolderListController extends BaseController {

    protected HelpFolderService helpFolderService;
    protected HelpFolder selectedHelpFolder;
    protected Listbox listboxHelpFolder;
    protected Search searchHelpFolder;
    protected List<HelpFolder> helpFolders;

    public HelpFolderService getHelpFolderService() {
        return this.helpFolderService;
    }

    public void setHelpFolderService(HelpFolderService helpFolderService) {
        this.helpFolderService = helpFolderService;
    }

    public HelpFolder getSelectedHelpFolder() {
        return this.selectedHelpFolder;
    }

    public void setSelectedHelpFolder(HelpFolder selectedHelpFolder) {
        this.selectedHelpFolder = selectedHelpFolder;
    }

    public List<HelpFolder> getHelpFolders() {
        return this.helpFolders;
    }

    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchHelpFolder = new Search(HelpFolder.class);
        this.searchHelpFolder.setMaxResults(100);
        this.searchHelpFolder.addFetch("createdBy");
        this.searchHelpFolder.addFetch("updatedBy");
        this.helpFolders = this.helpFolderService.search(this.searchHelpFolder);

        component.setAttribute("controller", this, false);
    }

    public void onClick$menuitemHelpFolderCreate(Event event) throws Exception {
        this.helpFolderCreate();
    }

    public void onClick$menuitemHelpFolderEdit(Event event) throws Exception {
        this.helpFolderEdit();
    }

    public void onClick$menuitemHelpFolderDelete(Event event) throws Exception {
        this.helpFolderDelete();
    }

    public void onClick$menuitemHelpFolderSearch(Event event) throws Exception {
        this.helpFolderSearch();
    }

    public void onClick$menuitemHelpFolderRefresh(Event event) throws Exception {
        this.helpFolderRefresh();
    }

    public void onClick$toolbarbuttonHelpFolderCreate(Event event) throws Exception {
        this.helpFolderCreate();
    }

    public void onClick$toolbarbuttonHelpFolderEdit(Event event) throws Exception {
        this.helpFolderEdit();
    }

    public void onClick$toolbarbuttonHelpFolderDelete(Event event) throws Exception {
        this.helpFolderDelete();
    }

    public void onClick$toolbarbuttonHelpFolderSearch(Event event) throws Exception {
        this.helpFolderSearch();
    }

    public void onClick$toolbarbuttonHelpFolderRefresh(Event event) throws Exception {
        this.helpFolderRefresh();
    }

    public void onDoubleClickListFolder(Listitem listitem) throws Exception {
        this.helpFolderEdit();
    }
    
    private void helpFolderCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/helpFolder.zul");
    }

    private void helpFolderEdit() throws Exception {
        if (this.selectedHelpFolder != null) {
            this.execution.sendRedirect("~./zul/html/helpFolder.zul?Id=" + this.selectedHelpFolder.getId().toString());
        }
    }

    private void helpFolderDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxHelpFolder.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    HelpFolder helpFolder = (HelpFolder) selectedItem.getValue();
                    this.helpFolderService.remove(helpFolder);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.helpFolders = this.helpFolderService.search(this.searchHelpFolder);
        this.binder.loadAll();
    }

    private void helpFolderSearch() throws Exception {
        this.execution.setAttribute("searchHelpFolder", this.searchHelpFolder);

        Window window = (Window) this.execution.createComponents("~./zul/html/helpFolderSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchHelpFolder = (Search) this.execution.getAttribute("searchHelpFolder");
        }

        this.helpFolders = this.helpFolderService.search(this.searchHelpFolder);
        this.binder.loadAll();
    }

    private void helpFolderRefresh() {
        this.helpFolders = this.helpFolderService.search(this.searchHelpFolder);
        this.binder.loadAll();
    }
}
